#!/bin/bash

# Replace with your container name
CONTAINER_NAME="mysql-hive"

# Function to check if a docker container is running
is_container_running() {
    container_name=$1
    if [ "$(docker ps -q -f name=^/${container_name}$)" ]; then
        return 0
    else
        return 1
    fi
}

# Step 1: Check for Docker
if ! command -v docker &> /dev/null; then
    echo "Docker is not installed. Please install Docker and try again."
    exit 1
fi

# Step 2: Check for Docker Compose
if ! command -v docker-compose &> /dev/null; then
    echo "Docker Compose is not installed. Please install Docker Compose and try again."
    exit 1
fi

# Step 3: Check if specified Docker container is running
if is_container_running $CONTAINER_NAME; then
    echo "Docker container '$CONTAINER_NAME' is already running."
    exit 1
else
    echo "Docker container '$CONTAINER_NAME' is not running. Proceeding..."
fi

# Step 4: Start the application using Docker Compose
echo "Starting the application with Docker Compose..."
docker-compose up -d

if [ $? -ne 0 ]; then
    echo "Failed to start the application with Docker Compose."
    exit 1
fi

echo "MySQL is starting, please wait a moment."

# Print the current server IP address
IP_ADDRESS=$(hostname -I | awk '{print $1}')

# 定义红色文字的ANSI转义序列
RED='\033[0;31m'
NC='\033[0m' # 没有颜色（用于重置颜色）
echo -e "${RED}首次安装需要等待MySQL初始化文件，约 3min 左右，通过 docker ps 可查看容器状态为 Up 5 minutes (healthy) 时后可正常连接。${NC}"

echo "================================================"
echo "'$CONTAINER_NAME' 连接信息如下"
echo "MySQL Host: $IP_ADDRESS"
echo "MySQL Port: 13308"
echo "MySQL User: root"
echo "MySQL Pwd : Hzsun@310012"
echo "================================================"